All files / web/src/app/api/households/[id] route.ts

0% Statements 0/91
0% Branches 0/1
0% Functions 0/1
0% Lines 0/91

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92                                                                                                                                                                                       
import { NextResponse } from 'next/server'
import { withAuth } from '@/lib/auth/withAuth'
import {
  getHouseholdDetail,
  getHouseholdSuggestions,
  isHouseholdMember,
  updateHouseholdName,
  transferHouseholdOwnership,
} from '@/lib/household'

/**
 * GET /api/households/[id]
 *
 * Get full details of a household including all members.
 * Only accessible to household members.
 */
export const GET = withAuth(
  async (_request, { userId, params }) => {
    const { id: householdId } = (await params) as { id: string }

    const isMember = await isHouseholdMember(householdId, userId)
    if (!isMember) {
      return NextResponse.json({ error: 'Not a member of this household' }, { status: 403 })
    }

    const [household, suggestions] = await Promise.all([
      getHouseholdDetail(householdId),
      getHouseholdSuggestions(householdId),
    ])
    if (!household) {
      return NextResponse.json({ error: 'Household not found' }, { status: 404 })
    }

    return NextResponse.json({ household, suggestions })
  },
  { role: 'user' }
)

/**
 * PATCH /api/households/[id]
 *
 * Update household. Supports:
 * - { name: string } — rename (owner only)
 * - { newOwnerId: string } — transfer ownership (owner only)
 */
export const PATCH = withAuth(
  async (request, { userId, params }) => {
    const { id: householdId } = (await params) as { id: string }

    const household = await getHouseholdDetail(householdId)
    if (!household) {
      return NextResponse.json({ error: 'Household not found' }, { status: 404 })
    }

    if (household.ownerId !== userId) {
      return NextResponse.json(
        { error: 'Only the owner can update the household' },
        { status: 403 }
      )
    }

    const body = await request.json()

    // Transfer ownership
    if (body.newOwnerId) {
      const result = await transferHouseholdOwnership(householdId, userId, body.newOwnerId)
      if (!result.success) {
        return NextResponse.json({ error: result.error }, { status: 400 })
      }
      const updated = await getHouseholdDetail(householdId)
      return NextResponse.json({ household: updated })
    }

    // Rename
    if (body.name !== undefined) {
      const name = typeof body.name === 'string' ? body.name.trim() : ''
      if (name.length === 0) {
        return NextResponse.json({ error: 'Name is required' }, { status: 400 })
      }
      if (name.length > 100) {
        return NextResponse.json({ error: 'Name must be 100 characters or less' }, { status: 400 })
      }
      await updateHouseholdName(householdId, name)
      const updated = await getHouseholdDetail(householdId)
      return NextResponse.json({ household: updated })
    }

    return NextResponse.json({ error: 'No valid fields to update' }, { status: 400 })
  },
  { role: 'user' }
)